
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from autoTest_ecshop.utils.log_util import logger
import traceback


class BasePage():

    def __init__(self,driver):
        self.driver =driver
        self.wait = WebDriverWait(driver,5)

    ## 定位单个元素
    def find_element(self,locator):
        try:
            logger.info(f"定位元素{locator}")
            element = self.wait.until(EC.visibility_of_element_located(locator))
            logger.info(f"元素{locator}定位成功")
            return element
        except:
            logger.error(f"元素定位失败，失败原因如下：\n{traceback.format_exc()}")
            raise

    ## 定位多个元素
    def find_elements(self,locator):
        try:
            logger.info(f"定位元素{locator}")
            elements = self.wait.until(EC.visibility_of_all_elements_located(locator))
            logger.info(f"元素{locator}定位成功")
            return elements
        except:
            logger.error(f"元素定位失败，失败原因如下：\n{traceback.format_exc()}")
            raise

    ## 输入数据
    def input_data(self,locator,value):
        # 定位到输入框
        inputBox = self.find_element(locator)
        # 输入数据
        inputBox.clear()
        logger.info(f"输入的数据是：\n{value}")
        inputBox.send_keys(value)

    ## 点击按钮
    def btn_data(self,locator):
        # 定位到按钮
        btnBox = self.find_element(locator)
        # 点击按钮
        btnBox.click()
        logger.info(f"点击了按钮：{locator}")

    def get_text(self,locator):
        logger.info(f"获取到了元素{locator}的文本信息")
        element = self.find_element(locator)
        logger.info(f"文本信息为：{locator}")
        return element.text




